package pl.edu.agh.uhm.serveragent;

import java.util.Dictionary;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.Set;

import org.osgi.framework.BundleContext;
import org.osgi.service.upnp.UPnPDevice;
import org.osgi.service.upnp.UPnPIcon;
import org.osgi.service.upnp.UPnPService;
import org.apache.felix.upnp.extra.util.UPnPEventNotifier;

import pl.edu.agh.uhm.serveragent.devices.vm.VMDevice;

public class HypervisorDevice implements UPnPDevice{

	
	final private String DEVICE_ID;
	final public static String DEVICE_ID_PREFIX="uuid:VMHypervisor:";
	
	private String[] vmUDNs;
	
	
	private HypervisorService hypervisorService;
	
	private UPnPService[] services;
	private Dictionary dictionary;

	public static UPnPEventNotifier notifier = null;

	public Dictionary getDictionary() {
		
		return dictionary;
		
	}

	
	

	
	public HypervisorDevice(BundleContext context, String id, List<String> vmUUIDs) {
		
		hypervisorService=new HypervisorService(this);	
		services = new UPnPService[]{hypervisorService};

		DEVICE_ID=DEVICE_ID_PREFIX+id;
		if(vmUUIDs!=null){
			vmUDNs=new String[vmUUIDs.size()];
			int i=0;
			for(String vmName:vmUUIDs){
				vmUDNs[i]=VMDevice.DEVICE_ID_PREFIX+vmName;
				i++;
			}
			
		}
		initDeviceProperties();	
		notifier = new UPnPEventNotifier(context,this,hypervisorService);
		
		
	}

	private void initDeviceProperties() {
		
		dictionary=new Properties();
		dictionary.put(UPnPDevice.UPNP_EXPORT, "");
		dictionary.put(
		        org.osgi.service.device.Constants.DEVICE_CATEGORY,
	        	new String[]{UPnPDevice.DEVICE_CATEGORY}
	    );
		dictionary.put(UPnPDevice.FRIENDLY_NAME,"Felix OSGi-UPnP Toy");
		dictionary.put(UPnPDevice.MANUFACTURER,"AGH UHD");
		dictionary.put(UPnPDevice.MANUFACTURER_URL,"http://agh.edu.pl/pp/uhd");
		dictionary.put(UPnPDevice.MODEL_DESCRIPTION,"UPnP Device representing VM Hypervisor");
		dictionary.put(UPnPDevice.MODEL_NAME,"VM Hypervisor");
		dictionary.put(UPnPDevice.MODEL_NUMBER,"1.0");
		dictionary.put(UPnPDevice.MODEL_URL,"http://agh.edu.pl/pp/uhd/VMhypervisor.html");
		dictionary.put(UPnPDevice.SERIAL_NUMBER,"123456789");
		dictionary.put(UPnPDevice.TYPE,"urn:schemas-upnp-org:device:VMhypervisor:1");
		dictionary.put(UPnPDevice.UDN,getDEVICE_ID());
		dictionary.put(UPnPDevice.UPC,"1213456789");
	//embedding - uncomment the line below to enable embedding device
		if(vmUDNs!=null)
			dictionary.put(UPnPDevice.CHILDREN_UDN,vmUDNs);
		//set also PARENT_UDN property in embedded device!
		
		
		Set types = new HashSet(services.length+5);
		String[] ids = new String[services.length];
		for (int i = 0; i < services.length; i++) {
			ids[i]=services[i].getId();
			types.add(services[i].getType());
		}
		
		dictionary.put(UPnPService.TYPE, types.toArray(new String[]{}));
		dictionary.put(UPnPService.ID, ids);
		
		
		
	}
	
	public Dictionary getDescriptions(String arg0) {
		return dictionary;
	}

	
	public UPnPIcon[] getIcons(String arg0) {
		UPnPIcon icon = new HypervisorIcon();
		return new UPnPIcon[]{icon} ;
	}

	
	public UPnPService getService(String serviceId) {
		if  (serviceId.equals(hypervisorService.getId())) return hypervisorService;
		else return null;
	}

	
	public UPnPService[] getServices() {
		return services;
	}





	public String getDEVICE_ID() {
		return DEVICE_ID;
	}

	
}
